package cn.pugle.oj.leetcode;

import cn.pugle.oj.catalog.StackProblem;

import java.util.LinkedList;

/**
 * @author tzp
 * @since 2020/9/24
 */
public class LC20 implements StackProblem {
    public boolean isValid(String s) {
        if (s.length() == 0) return true;
        LinkedList<Character> stack = new LinkedList<>();
        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);
            switch (c) {
                case '[':
                case '{':
                case '(': {
                    stack.offerLast(c);
                    break;
                }
                case ')': {
                    Character pop = stack.pollLast();
                    if (pop == null || !pop.equals('(')) return false;
                    break;
                }
                case '}': {
                    Character pop = stack.pollLast();
                    if (pop == null || !pop.equals('{')) return false;
                    break;
                }
                case ']': {
                    Character pop = stack.pollLast();
                    if (pop == null || !pop.equals('[')) return false;
                    break;
                }
                default:
                    return false;
            }
        }
        return stack.size() == 0;
    }

    public static void main(String[] args) {
//        System.out.println(new LC20().isValid("(())"));
//        System.out.println(new LC20().isValid("()()"));
//        System.out.println(new LC20().isValid("()(()"));
        System.out.println(new LC20().isValid("()[]{}"));
//        System.out.println(new LC20().isValid("()[{]}"));
    }
}
